package com.google.gautoard.mapreduce;

import com.google.appengine.tools.mapreduce.Mapper;
import com.google.gautoard.model.Reading;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import static com.google.gautoard.OfyService.ofy;
import com.google.gautoard.model.Sensor;

/**
 *
 * @author dras
 */
public class TemperatureReadingMapper extends Mapper<Reading, String, String> {

  private static final Logger log = Logger.getLogger(TemperatureReadingMapper.class.getName());
  long cur = System.currentTimeMillis();

  public void map(Reading reading) {
    Sensor sensor = ofy().load().now(reading.getSensor());
    if (sensor.getType() != Sensor.Type.TEMPERATURE) {
      return;  // ignore non-temperature readings
    }
    Long key = sensor.getId();
    String sentitydate = getDateString(reading.getTimestamp());
    String value = (String) reading.getValue(); // value of reading
    emit(key + ":" + sentitydate, value);
    // emits the form: com.rasdesign.temperature...:20140417, 72
  }

  public synchronized String getDateString(Date d) {
    // entityday should be formatted as yyyymmdd, like 20140214 for feb 14, 2014
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    return formatter.format(d);
  }
}
